package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;

    public void start(){
        /*System.out.println("UserController:开始处理用户请求");
        System.out.println("UserController:开始处理注册");
        System.out.println("UserController:获取用户输入信息...");
        userMapper.insert(null);
        System.out.println("UserController:注册成功");*/
        Scanner sc = new Scanner(System.in);
        System.out.println("请选择操作：1.注册 2.登录 3.列出用户列表 4.修改用户信息");

        int num=sc.nextInt();
        switch (num){
            case 1:
                userReg();
                break;
            case 2:
                userLogin();
                break;
            case 3:
                userList();
                break;
            case 4:
                userUpdate();
                break;
            default:
                System.out.println("输入错误");
        }
    }
    private void userUpdate(){
        System.out.println("开始修改用户信息");
        User user = new User();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        user.setUsername(sc.next());
        System.out.println("请输入密码：");
        user.setPassword(sc.next());
        System.out.println("请输入昵称：");
        user.setNickname(sc.next());
        System.out.println("请输入年龄：");
        user.setAge(sc.nextInt());
        System.out.println(user);
        int result = userMapper.update(user);
        System.out.println(result>0?"修改成功":"修改失败");
    }


    private void userReg(){
        System.out.println("开始注册");
        User user = new User();
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        user.setUsername(sc.next());
        System.out.println("请输入密码：");
        user.setPassword(sc.next());
        System.out.println("请输入昵称：");
        user.setNickname(sc.next());
        System.out.println("请输入年龄：");
        user.setAge(sc.nextInt());
        System.out.println(user);

        /*int result = userMapper.insert(user);
        System.out.println(result>0?"注册成功":"注册失败");*/
        if (userMapper.selectByUsername(user.getUsername())==null){
            int result = userMapper.insert(user);
            System.out.println(result>0?"注册成功":"注册失败");
        }else {
            System.out.println("用户已存在");
        }
    }
    private void userLogin(){
        System.out.println("开始登录");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username=sc.next();
        System.out.println("请输入密码：");
        String password=sc.next();

        User user = userMapper.selectByUsername(username);
        System.out.println( user);
        if(user!=null){
            if (user.getPassword().equals(password)){
                System.out.println("登录成功,欢迎【"+user.getNickname()+"】回来！");
            }else {
                System.out.println("密码错误");
            }

        }else {
            System.out.println("用户["+username+"]不存在");
        }
    }
    private void userList(){
        System.out.println("开始列出用户列表");
        List<User> list = userMapper.selectAll();
        for (User user:list){
            System.out.println(user);
        }
    }
}
